
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class AlbumHelper {
    
    public static File songbirdToJavaFolder(String folderpath)
    {
        try {
            String folder = folderpath.substring(0, folderpath.lastIndexOf("/"));
            return new File(new URI(folder));
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    public static File songbirdToJavaFile(String path,String filename, String filetype)
    {   
      
        String temp = "";
        char[] c = filename.trim().toCharArray();
        for (int i = 0 ; i < c.length; i++)
        {
            if (Character.isJavaIdentifierPart(c[i]) || c[i] == ' ')
                    temp += c[i];
            else
                temp += "%" + Integer.toHexString(Character.codePointAt(c, i));
        }
        
        try{
            return new File(songbirdToJavaPath(path, temp, filetype));
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(path + temp + '.' + filetype);
            return null;
        }
        
        
    }
     public static URI songbirdToJavaPath(String path) {
        try {
            String temp;
            int index = path.lastIndexOf("/");
            temp = path.substring(0, index + 1);
            return new URI(temp);
        } catch (URISyntaxException ex) {
           ex.printStackTrace();
           return null;
        }
     }
    public static URI songbirdToJavaPath(String path, String filename,String filetype) {

        URI uri = null;
        String temp;
        int index = path.lastIndexOf("/");
        temp = path.substring(0, index + 1);
        
        char[] c = filename.trim().toCharArray();
        String tempFilename = "";
        /*
        if (temp.length() > 6)
        {
        temp = temp.substring(7);
        String[] tempAr = temp.split("%");
        temp = tempAr[0];
        char c;
        byte[] b = new byte[1];
        for(int i = 1; i < tempAr.length; i++)
        {
        b[0]=(byte)Integer.parseInt(tempAr[i].substring(0,2),16);
        try{
        temp = temp + new String(b,"ISO-8859-1") + tempAr[i].substring(2);
        }
        catch(Exception e){System.out.println(e);}
        }
        }
        else
        {
        temp = str;
        }
         */

        for (int i = 0; i < c.length; i++) {
            
            tempFilename += "%" + Integer.toHexString(Character.codePointAt(c, i));
         }



        try {
            uri = new URI(temp + tempFilename + "." + filetype);
        } catch (Exception ue) {
            ue.printStackTrace();
            System.out.println(ue);
            System.out.println(temp + tempFilename + filetype);
        }
        //return temp;
        return uri;
    }
    
    /**
     * Creates a Java file based on the path given. 
     * @param path
     * Must be in a form that directly converts to a URI. Else use 
     * songbirdToJavaFile(String path,String filename)
     * @return the file for the given filepath.
     */
    public static File songbirdToJavaFile(String path) {
        try {


            return new File(new URI(path));
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static BufferedImage scale(BufferedImage image, int requestedThumbSize) {
        float ratio = (float) image.getWidth() / (float) image.getHeight();
        int width = image.getWidth();
        BufferedImage thumb = image;

        do {
            width /= 2;
            if (width < requestedThumbSize || requestedThumbSize > image.getWidth()) {
                width = requestedThumbSize;
            }

            BufferedImage temp = new BufferedImage(width, (int) (width / ratio), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = temp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
            g2.dispose();

            thumb = temp;
        } while (width != requestedThumbSize);

        return thumb;
    }

    public static BufferedImage loadCompatibleImage(File resource) throws IOException {
        BufferedImage image = ImageIO.read(resource);
        GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage compatibleImage = configuration.createCompatibleImage(image.getWidth(),
                image.getHeight());
        Graphics g = compatibleImage.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return compatibleImage;
    }

    public static void fileCopy(File from, File to) {
        
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            if (to.exists()) {
                to.delete();
            }
            to.createNewFile();
            in = new FileInputStream(from);
            
            out = new FileOutputStream(to);
            
            int i;
            
            byte[] b = new byte[1024];
            
            
            
            while ((i = in.read(b)) != -1) {
                out.write(b, 0, i);
                
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException ex) {
               ex.printStackTrace();
            }
        }
    }
}
	